<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'buyland';
$return=array();
$return['setting']=$this->plugin_setting('basic');
$month=array('1','3','6','12');
if($_RQ['op']=='buyland'){
	//租赁土地
	$area = intval($_RQ['area']);
	if(!$area){
		exi('没有选择购买面积?','error','jy_farmer/lclassify/list');
	}
	$id = intval($_RQ['id']);
	if(!$id){
		exi('没有找到该土地，请重新选择！','error','jy_farmer/lclassify/list');
	}
	$land=pdo_get('jy_farmer_land',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$land['area']=$area;
	$lclassify=pdo_get('jy_farmer_lclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$land['classify_id']));
	$land['price']=$lclassify['price'];
	$this->session('land_id',$_RQ['id']);
	$return['land']=$land;
	$return['month']=$month;
	$return['lclassify']=$lclassify;
	$return['post']=array('total'=>1,'pcd'=>array('city'=>'深圳市'));
	$return['post']['month']=1;
	$return['post']['area']=$area;
	$return['post']['money']=$land['price']*$area*1;
	$order=pdo_get('jy_farmer_order',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('username','mobile'),'id desc');
	$return['post']['username']=$order['username'];
	$return['post']['mobile']=$order['mobile'];
	exi($return);
}

if($_RQ['op']=='notice'){
	$post=get_postdata();
	$land_id = $this->session('land_id');
	if($post){
			$land=pdo_get('jy_farmer_land',array('uniacid'=>$_SESSION['uniacid'],'id'=>$land_id));
			$lclassify=pdo_get('jy_farmer_lclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$land['classify_id']));
			if(empty($post['username'])){
				exi('请输入姓名','error');
			}
			if(empty($post['mobile'])){
				exi('请输入联系电话','error');
			}
			if(!preg_match("/^1[345789]\d{9}$/ims",$post['mobile'])){  
	 
				exi('请输入正确的手机号码','error');
			}
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'land_id'=>$this->session('land_id'),
				'username'=>$post['username'],
				'mobile'=>$post['mobile'],
				'address'=>$post['pcd']['province'].' '.$post['pcd']['city'].' '.$post['pcd']['district'].' '.$post['pcd']['street'].' '.$post['address'],
				'time'=>$post['month'],
				'num'=>$post['area'],
				'money'=>$post['area']*$post['month']*$lclassify['price'],
				'ordersn'=>get_ordersn('jy_farmer_order','ordersn'),
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_farmer_order',$insert);
			$id=pdo_insertid();		
			$params['fee']=$post['area']*$post['month']*$lclassify['price'];
			$params['title']='租赁'.$land['title'];
			$params['id']=$id;
			$params['plugin']='jy_farmer';
			$params['back_url']='jy_farmer/buyland/payresult';
			$payid=cfc('pay')->create_pay_order($params);
			$this->pdo_update('order',array('pay_id'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
	}
   else{
	   exi('获取数据失败','error');
   }
	
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_farmer_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_farmer_order',array('status'=>1,'pay'=>$payresult['fee']),array('id'=>$order['id']));
	$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'land_id'=>$order['land_id'],
		'order_id'=>$order['id'],
		'plantarea'=>$order['num'],
		'createtime'=>TIMESTAMP,	
	);
	pdo_insert('jy_farmer_plant',$insert);
	//检查地块剩余面积
	$unarea=pdo_getcolumn('jy_farmer_land',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['land_id']),'unarea');
	$unarea=$unarea-$order['num'];
	if($unarea<=0){
		$issell=1;
	}else{
		$issell=0;
	}
	pdo_update('jy_farmer_land',array('issell'=>$issell,'unarea'=>$unarea),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['land_id']));
	$this->model('order')->pay($order);
	$this->model('notice')->notice_payorder($order);
	exi('支付成功','success','jy_farmer/myplant/unplant/');
}
if($_RQ['op']=='money'){
	if($_RQ['id']===''){
		exi('请选择土地','error');
	}
	if($_RQ['time'] == ''){
		exi('请重新选择租赁时长','error');
	}
	$land=pdo_get('jy_farmer_land',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$lclassify=pdo_get('jy_farmer_lclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$land['classify_id']));
	$money=$_RQ['area']*$_RQ['time']*$lclassify['price'];
	$return['money']=$money;
	$return['success']=1;
	exi($return);
}